import 'babel-polyfill'

// 如果你想用自己创建的实例，在自己单独的js文件里加上：let MOUNT_PATH="noNeed" / localStorage.setItem('MOUNT_PATH',MOUNT_PATH)
const MOUNT_PATH = localStorage.getItem('MOUNT_PATH')
console.log('VUE File:', MOUNT_PATH)

// TODO: 异步加载
if (IS_FROM_WX) {
  require.ensure(['@utils/wechat'], (require) => {
    const wechat = require('@utils/wechat')['default']
    wechat.init()
  }, 'wechat')
}

if (MOUNT_PATH === 'noNeed') {
  // 组件自己创建了实，将不会再引入下边的资源
  throw new Error("当前组件自己创建了实例,不会引用公用方法、实例");
}

import {
  beforeRouteEnter,
} from '@router/path'
if (!beforeRouteEnter())
  throw new Error("不允许访问");

import Vue from 'vue';
Vue.config.productionTip = false

import {
  IMG_URL,
} from '@utils/default_config'

import {
  Lazyload
} from 'vant'
import '@vant/touch-emulator';
import '@style_m/basic'

// 自动计算宽高匹配默认值
Vue.use(Lazyload, {
  loading: '/static/images/common/default/lazyload-img-3.png',
  error: '/static/images/common/default/lazyload-img-3.png',
  preLoad: 1.3,
  filter: { // 目前因为后端api接口返回的图片不带域名，所以添加了下，要跟后端的说下，必须返回带域名的图片url
    progressive(listener, options) {
      const index = listener.src ? listener.src.indexOf('uploads') : -1
      const origin = index === 0 ? (IMG_URL + '/') : (index === 1 ? IMG_URL : '')
      if (origin) {
        listener.src = origin + listener.src
      }
    },
  },
  adapter: {
    loading(listender, Init) {
      const el = listender.el,
        height = el.clientHeight,
        width = el.clientWidth,
        scale = height / width
      let lazypng = 3
      switch (scale) {
        case (1):
          lazypng = 1
          break
        default:
          lazypng = 3
          break
      }

      listender.loading = listender.error = `/static/images/common/default/lazyload-img-${lazypng}.png`
    }
  }
});

// 复制到粘贴板
import VueClipboard from 'vue-clipboard2'
VueClipboard.config.autoSetContainer = true
Vue.use(VueClipboard)

// 挂载js方法到vue
import {
  IS_FROM_APP,
  IS_FROM_WX,
  setPageTitle,
} from '@utils/utils'
import {
  getUrlParam,
  mergePageParams,
  goPageBack,
  goPage,
} from '@router/path'

setPageTitle()

Vue.prototype.$is_from_app = IS_FROM_APP
Vue.prototype.$is_from_wx = IS_FROM_WX
Vue.prototype.$set_page_title = setPageTitle
Vue.prototype.$set_share = (params) => {
  if (IS_FROM_WX) {
    require.ensure(['@utils/wechat'], (require) => {
      const wechat = require('@utils/wechat')['default']
      wechat.share(params)
    }, 'wechat')
  } else if (IS_FROM_APP) {
    require.ensure(['@utils/appBridge'], (require) => {
      const {
        appShareInfo
      } = require('@utils/appBridge')
      appShareInfo(params)
    }, 'appBridge')
  }
}
Vue.prototype.$getUrlParam = getUrlParam
Vue.prototype.$mergePageParams = mergePageParams
Vue.prototype.$goPageBack = goPageBack
Vue.prototype.$goPage = goPage

// 动态捕获要挂载实例的.vue文件的路径
var App = resolve => require.ensure([], () => resolve(require("../src/views/" + MOUNT_PATH + ".vue")));
new Vue({
  el: '#app',
  template: '<App/>',
  components: {
    App
  },
})